//
//  DataBaseManager.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/29.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "DataBaseManager.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"

@interface DataBaseManager ()
@property(nonatomic, retain)FMDatabase *fmdb;
@end
@implementation DataBaseManager

+(DataBaseManager *)shareInstance
{
    static DataBaseManager *dbmanager = nil;
    if (!dbmanager) {
        dbmanager =[[ DataBaseManager alloc] init];
    }return dbmanager;
}

//打开数据库
-(void)openDB
{
    //获取数据库路径
    NSString *path =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"fmdb.db"];
    NSLog(@"dbpath: %@",path);
    //创建数据库对象
    self.fmdb = [[ FMDatabase alloc] initWithPath:path];
    //打开数据库
    BOOL result = [_fmdb open];
    //判断是否打开数据库
    if (result) {
        NSLog(@"数据库打开成功");
    }
    else
    {
        NSLog(@"数据库开启失败");
    }
}
//创建表
-(void)createTable
{ NSString *createSQL = @"CREATE TABLE IF NOT EXISTS kitchen (number integer PRIMARY KEY AUTOINCREMENT, name text, url text, h_id text)";
   [self executeSQL:createSQL message:@"创建表"];
}
//删除表
-(void)dropTable
{
    NSString *dropSQL =@"DROP TABLE kitchen";
    [self executeSQL:dropSQL message:@"删除表"];
}
//加
-(void)insertfood:(FoodModel *)food
{
    NSString *inserSQL = [ NSString stringWithFormat:@"INSERT INTO kitchen(name,url,h_id) values('%@','%@','%@')",food.dashes_name, food.share_url,food.dishes_id];
    [self executeSQL:inserSQL message:@"插入"];
    
}
//删
-(void)deletename:(NSString *)dashes_name
{
    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM kitchen WHERE name = '%@'",dashes_name];
    
    [self executeSQL:deleteSQL message:@"删除"];
}
//修改
//-(void)updateLanou:(LRlanou *)newl num:(NSInteger)num
//{
//    NSString *updateSQL = [NSString stringWithFormat:@"UPDATE lanou151129 SET name = '%@', gender = '%@' WHERE number = %ld", newl.name,newl.gender,num];
//    [self executeSQL:updateSQL message:@"修改"];
//    
//}
- (NSMutableArray *)select
{//返回需要返回的数据
    NSMutableArray *arr =[ NSMutableArray array];
    NSString *selectSQL = @"SELECT *FROM kitchen";
    //执行查询
    FMResultSet *resultset = [_fmdb executeQuery:selectSQL];
    //resultset 表示查询到的数据 通过遍历每一行的信息 然后逐列获取数据
    while ([resultset next]) {
        //获取每一列
        // number第0列
        //获取方式: 列名  列号
        NSString *name =[ resultset stringForColumn:@"name"];
        NSString *url =[ resultset stringForColumn:@"url"];
        NSString *h_id = [ resultset stringForColumn:@"h_id"];
        //创建对象 保存信息
       FoodModel *l = [[FoodModel alloc ] init];
        l.dashes_name = name;
        l.share_url = url;
        l.dishes_id = h_id;
        //添加到数组
        [arr addObject:l];
    }
    return arr;
}
//封装:执行+判断
//参数1:需要执行的SQL语句
//参数2; 执行之后的打印信息
-(void)executeSQL:(NSString *)sql message:(NSString *)mes
{ //执行
    BOOL result =[ _fmdb executeUpdate:sql];
    if (result) {
        NSLog(@"%@成功",mes);
    }else
    {
        NSLog(@"%@失败", mes);
    }
    
}


@end
